package com.example.mvvmtest.model.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.example.mvvmtest.model.entity.User
import kotlinx.coroutines.flow.Flow


@Dao
interface UserDao {

    // 用户注册
    @Insert
    suspend fun registerUser(user: User)

    // 验证该账号是否已被注册
    @Query("SELECT EXISTS(SELECT id FROM users WHERE id = :id)")
    suspend fun checkIsRegister(id: String): Boolean

    // 登录验证
    @Query("SELECT * FROM users WHERE id = :id AND password = :password LIMIT 1")
    suspend fun checkLogin(id: String, password: String): User?

    // 查询用户所有信息
    @Query("SELECT * FROM users WHERE id = :id LIMIT 1")
    fun getUserInfo(id: String): Flow<User?>

    @Query("UPDATE users SET password = :newPassword WHERE id = :id")
    suspend fun updatePassword(id: String, newPassword: String)

    // 更新年龄
    @Query("UPDATE users SET age = :newAge WHERE id = :id")
    suspend fun updateAge(id: String, newAge: Int)

    // 更新职业
    @Query("UPDATE users SET job = :newJob WHERE id = :id")
    suspend fun updateJob(id: String, newJob: String)


}